package edu.ciee.app.jichuxinxi.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import edu.ciee.app.action.util.JdbcUtil;
import edu.ciee.app.action.util.SessionUtil;

public class YaowuAction {

	/*
	 * ID int auto_increment, laiyuan varchar(50), product_type varchar(20),
	 * yingye_code varchar(50),
	 */

	private int id;
	private int laiyuan;
	private String name;
	private String info;
	private String tip;
	private List resultList;

	public List getResultList() {
		return resultList;
	}

	public void setResultList(List resultList) {
		this.resultList = resultList;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public int getLaiyuan() {
		return laiyuan;
	}

	public void setLaiyuan(int laiyuan) {
		this.laiyuan = laiyuan;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public String insert() throws Exception {

		String sql = "insert into yaowu_table(laiyuan,name,info) values("
				+ "\'" + this.getLaiyuan() + "\'," + "\'" + this.getName()
				+ "\'," + "\'" + this.getInfo() + "\');";

		System.out.println(sql);
		if (JdbcUtil.updateResult(sql) == 0) {
			this.setTip("\n" + this.getTip()
					+ "No change was made, probably duplicate names.");
			return "error";
		} else {
			ActionContext.getContext().getSession().remove("yaowumap");
			SessionUtil.updateSession("yaowu_table", "name", "yaowumap");

			this.setTip("Success. " + this.getLaiyuan() + " added.");
			return "success";
		}
	}
	
	public String search() {

		String query = "where 1=1";

		if (this.getId()!=0) {
			query = query + " and ID like '%" + this.getId() + "%'";// this.getID()
		}
		if (this.getName()!= null) {
			query = query + " and name like '%" + this.getName() + "%'"; // this.getName()
		}
		if (this.getLaiyuan()!= 0) {
			query = query + " and laiyuan like '%" + this.getLaiyuan() + "%'"; // this.getName()
		}
		if (this.getInfo()!= null) {
			query = query + " and info like '%" + this.getInfo() + "%'"; // this.getInfo()
		}
		
		String sql = "select * from yaowu_table " + query;
		System.out.println(sql);
		try {
			resultList = JdbcUtil.searchResult(sql);
			return "success";
		} catch (Exception e) {
			return "error";
		}
	}
	
	public String delete() {

		HttpServletRequest request = ServletActionContext.getRequest();
		ActionContext ctx = ActionContext.getContext();
		Map m = ctx.getParameters();
		String id = request.getParameter("id");
		System.out.println(id);
		String sql = "delete from yaowu_table where ID=" + id + ";";
		System.out.println(sql);
		try {
			JdbcUtil.updateResult(sql);
			ActionContext.getContext().getSession().remove("yaowumap");
			SessionUtil.updateSession("yaowu_table", "name", "yaowumap");
			return "refresh";
		} catch (Exception e) {
			this.setTip("error" + e.toString());
			return "error";
		}

	}
	
	public String forEdit(){
		HttpServletRequest request = ServletActionContext.getRequest(); 
		ActionContext ctx = ActionContext.getContext();		
		Map m = ctx.getParameters();		
		String name=request.getParameter("name");
		int id=Integer.parseInt(request.getParameter("id"));
		String info=request.getParameter("info");
		int laiyuan=Integer.parseInt(request.getParameter("laiyuan"));
		System.out.println(name);
		System.out.println("id="+id);
		System.out.println("info="+info);
//		this.setName(name);
//		this.setId(id);
//		this.setInfo(info);
//		this.setLaiyuan(laiyuan);
				
		return "success";
	}
	
	public String edit(){
		
		String sql="update yaowu_table set name = \'" + this.getName() +
				"\',laiyuan="+this.getLaiyuan()+
				",info=\'"+this.getInfo()+
				"\' where ID="+this.getId()+";";
		System.out.println(sql);
		try{
			JdbcUtil.updateResult(sql);
			ActionContext.getContext().getSession().remove("yaowumap");
			SessionUtil.updateSession("yaowu_table", "name", "yaowumap");
			return "refresh";
		}catch(Exception e){
			return "error";
		}
	}

}
